var ajPush = function(){

	this.push = api.require('ajpush');
	
	
}
//初始化推送服务，只Android有效，ios上会自动初始化
ajPush.prototype.action = function(){
	
	this.push.init(function(ret) {
	    if (ret && ret.status){
		   return;	          
	    }
	});
	//this.setListener();
	
}
ajPush.prototype.setListener = function(){
//设置消息监听，若iOS应用在前台运行，此时收到推送后也通过此方法回调
	this.push.setListener(	
	    function(ret) {
	         //alert(JSON.stringify(ret))
	         var ah = api.require('ajpush');
				ah.setBadge({
				    badge:0
				});
	       	 //return;
	         //var id = ret.id;
	         //var title = ret.title;
	         //var content = ret.content;
	         //var extra = ret.extra;
	    }
	);
}
ajPush.prototype.clearNotification = function(){

		var param = {id:-1};
		this.push.clearNotification(param,function(ret) {
		    if(ret && ret.status){
		        //success
		        //alert("clear")
		    }
		});
}

ajPush.prototype.setBadge = function(){
	this.push.setBadge({
		badge:0
	});
}

ajPush.prototype.getRegistrationId = function(){
	this.push.getRegistrationId(function(ret) {
		var registrationId = ret.id;
		$api.setStorage('registrationId',registrationId);
		//return;
	});
}



//监听点击通知事件
ajPush.prototype.addEventListener = function(){
if (api.systemType == "android") {
		api.addEventListener({
		    name: 'appintent'
		}, function(ret, err) {
		    if (ret && ret.appParam.ajpush) {
			   
		        var ajpush = ret.appParam.ajpush;
		        var id = ajpush.id;
		        var title = ajpush.title;
		        var content = ajpush.content;
		        var extra = ajpush.extra;
		       
				var extraObj = $api.strToJson(extra);    
				//alert(JSON.stringify(extraObj))
		     if(extraObj&&extraObj!=''&&typeof(extraObj)!=undefined){
		     	if(extraObj.sid){
		        	_openGoodsDetail(extraObj.sid)
		        }
		     }
		        
		        
		        //_openHeader('message')
		    }
		})
	}else if(api.systemType == "ios"){
	
		api.addEventListener({
		    name: 'noticeclicked'
		}, function(ret, err) {
		var ah = api.require('ajpush');
				ah.setBadge({
				    badge:0
				});
		    if (ret && ret.value) {
		    	
		        var ajpush = ret.value;
		        var content = ajpush.content;
		        var extra = ajpush.extra;
		        var extraObj = $api.strToJson(extra);   
		        
		     
		        if(extraObj&&extraObj!=''&&typeof(extraObj)!=undefined){
		     	if(extraObj.sid){
		        	_openGoodsDetail(extraObj.sid)
		        }
		     }
		    }
		})
	}
	
}

